<?php
/**
 * Created by PhpStorm.
 * User: xuic
 * Date: 2016/5/28
 * Time: 21:47
 */
namespace work {

    use app\ctr;
    use C\lib\Db;
    use C\mod\bill;

    // --- 城市支线(司机) ---

    class site extends ctr {

        // --- 站点(收件)列表1 , 已收包集 ---
        public function scanBagOkList() {

            if(($post = $this->getAesPost()) && ($worker = $this->getLogWorker())) {

                if (isset($post['okLastId'])) {
                    $lastId1 = $post['okLastId'] + 0;
                    $lastId1 = $lastId1 <= 0 ? 0 : $lastId1;
                } else {
                    $lastId1 = 0;
                }
                if($lastId1 == 0) $sqlLimit1 = ' cdb.id > 0 ';
                else  $sqlLimit1 = ' cdb.id < '.$lastId1;

                $stmt = Db::query('SELECT type FROM ' . DB_PRE . 'worker_type WHERE worker_id =' . $worker->id . ' and type = 3');

                if($stmt && $stmt->rowCount() > 0) {

                    $stmt2 = Db::query('SELECT cdb.id id ,  cdb.code code , cdc.name as cityName , count(cdo.id) pieceCount ,cdb.commission commission, if(isNull(cdu.phone),"",cdu.phone) phone FROM cd_order_piece cdo , cd_city  cdc , cd_bag cdb  LEFT JOIN  cd_user cdu ON cdb.user_id=cdu.id  WHERE  cdc.id=cdo.city_id_recipient AND (state_in=5 or state_in=6) AND cdo.worker_id=' . $worker->id . '  AND cdb.id=cdo.bag_id  AND cdb.state<=2  AND '.$sqlLimit1.'   GROUP BY cdo.bag_id  ORDER BY cdb.id DESC LIMIT  10    '); //  5 =>到站点，表已收包

                    $jbags = [];
                    if($stmt2 && $stmt2->rowCount() > 0) {
                        while ($data = $stmt2->fetch(\PDO::FETCH_ASSOC))
                            $jbags[] = $data; // 已收包集
                    }

                    $this->writeAesJson( 1 , ['jbags' => $jbags] , 1);
                } else
                    $this->writeAesJson( 0 , '非集结点' ,1 );
            }

        }

        // --- 站点(收件)列表2 , 即将收录件 ---
        public function scanBagWillList() {

            if(($post = $this->getAesPost()) && ($worker = $this->getLogWorker())) {
                if( isset($post['siteId']) && ($post['siteId'] != '') ) {

                    $stmt = Db::query('SELECT type FROM ' . DB_PRE . 'worker_type WHERE worker_id =' . $worker->id . ' and type = 3');

                    if($stmt && $stmt->rowCount() > 0) {

                        if(isset($post['willLastId'])) {
                            $lastId2 = $post['willLastId'] + 0;
                            $lastId2 = $lastId2 <= 0 ? 0 : $lastId2;
                        } else {
                            $lastId2 = 0;
                        }
                        if($lastId2 == 0) $sqlLimit2 = ' cdo.city_id_recipient > 0 '; else  $sqlLimit2 = ' cdo.city_id_recipient < ' . $lastId2;

                        $stmt2 = Db:: query('SELECT  cdo.city_id_recipient id , cdc.name cityName , count(distinct(cdo.id)) count FROM cd_order_piece cdo , cd_city cdc , cd_site cds , cd_site_worker cdsw WHERE  cdo.worker_id=' . $worker->id . ' AND cdo.city_id_recipient=cdc.id AND state_in=4 AND cds.id=cdsw.site_id AND cdsw.site_id='. $post['siteId'] .' AND cds.city_id=cdo.city_id_sender AND ' . $sqlLimit2 . ' GROUP BY cdo.city_id_recipient  ORDER BY id DESC LIMIT  10   '); // state_in=4 表即将收录件

                        $jPieces = [];
                        if($stmt2 && $stmt2->rowCount() > 0) {
                            while($data = $stmt2->fetch(\PDO::FETCH_ASSOC)) $jPieces[] = $data; // 即将收录件
                        }

                        $this->writeAesJson(1, ['jpieces' => $jPieces], 1);
                    } else
                        $this->writeAesJson(0, '非集结点', 1);
                } else
                    $this->writeAesJson(0, '无当前所处站点ID提交', 1);
            }

        }



        // --- 接收站点扫包装件 ---

        public function scanBagAdd() {

            if(($post = $this->getAesPost()) && ($worker = $this->getLogWorker())) {

                $stmt = Db::query('SELECT type   FROM cd_worker_type WHERE worker_id =' . $worker->id . ' AND type=3');

                if($stmt && $stmt->rowCount() > 0) {

                    if( isset($post['itemCodes']) && count($post['itemCodes']) ) {
                        if( isset($post['bagCode']) && trim($post['bagCode']) != '' ) {

                                if(isset($post['fromSiteId']) && $post['fromSiteId'] != '') {
                                    if(isset($post['siteId']) && $post['siteId'] != '') {
                                        if( $post['siteId'] != $post['fromSiteId'] ) {

                                            if(isset($post['startTime']) && $post['startTime'] != '') {
                                                if(isset($post['endTime']) && $post['endTime'] != '') {
                                                    date_default_timezone_set('PRC');
                                                    if((strtotime($post['endTime']) + 10) > $_SERVER['REQUEST_TIME'] && (strtotime($post['startTime']) + 10) > $_SERVER['REQUEST_TIME']) {//避免延时，允许误差１０秒，
                                                        if(strtotime($post['endTime']) > strtotime($post['startTime'])) {

                                                            if(isset($post['commission']) && is_numeric($post['commission']) && $post['commission'] >= 0) {
                                                                $sacks = [];
                                                                $pieces = [];
                                                                foreach($post['itemCodes'] as $v) {
                                                                    if(strtolower($v[0]) == 'p') $pieces[] = $v;
                                                                    else if(strtolower($v[0]) == 's') $sacks[] = $v;
                                                                }

                                                                if(count($sacks)) {
                                                                    // --- 有袋号时 ---
                                                                    // --- 查袋ID --
                                                                    $strSackCodes = '"' . implode('","', $sacks) . '"';
                                                                    $stmt1 = Db::query('SELECT id , code FROM ' . DB_PRE . 'sack WHERE  is_finish=0 AND count!=0 AND code!="" AND code IN (' . $strSackCodes . ') ');
                                                                    if($stmt1 && $stmt1->rowCount() > 0) {
                                                                        while($data = $stmt1->fetch(\PDO::FETCH_ASSOC)) {
                                                                            $sackIds[] = $data['id']; // 取袋ID
                                                                            $sackCodes[] = strtoupper($data['code']); // 取合法袋code
                                                                        }

                                                                        $result = array_diff($sacks, $sackCodes);

                                                                        if(!$result) {
                                                                            $strPieceCodes = '"' . implode('","', $pieces) . '"';
                                                                            $stmt1 = Db::query('SELECT piece_code , city_id_recipient , city_id_sender FROM ' . DB_PRE . 'order_piece WHERE state_in<=4 AND state_in>=2 AND   piece_code!="" AND piece_code IN (' . $strPieceCodes . ')'); //查询过滤符合要求的件码

                                                                            if($stmt1 && $stmt1->rowCount() >= 0) {
                                                                                $pieceArr = [];
                                                                                while($data = $stmt1->fetch(\PDO::FETCH_ASSOC)) $pieceArr[] = strtoupper($data['piece_code']);

                                                                                $result1 = array_diff($pieces, $pieceArr);
                                                                                if(!$result1) {

                                                                                    $strSackIds = '"' . implode('","', $sackIds) . '"';
                                                                                    $stmt1 = Db::query('SELECT piece_code FROM ' . DB_PRE . 'order_piece WHERE state_in<=4 AND state_in>=2 AND  sack_id IN (' . $strSackIds . ') '); //查询过滤符合要求的件

                                                                                    if($stmt1 && $stmt1->rowCount() > 0) {
                                                                                        while($data = $stmt1->fetch(\PDO::FETCH_ASSOC)) $pieceArr[] = strtoupper($data['piece_code']);
                                                                                        Db::beginTransaction();

                                                                                        $pieceArr = array_unique($pieceArr);


                                                                                        $bag = new \C\mod\bag();

                                                                                        // --- 判断包码是否存在 ---
                                                                                        if( $b=$bag->get( [['code','=',$post['bagCode']],['state' ,'in',[0,1]]])) {
                                                                                            // --- 已在 ---
                                                                                            $pre = Db::prepare('UPDATE cd_bag SET count=count+ :newCount WHERE id=:id');

                                                                                            if($pre->execute([':id'=> $b->id , ':newCount'=> count($pieceArr)] )) $k = 0;
                                                                                            else $k = 4;
                                                                                            $bag = $b;
                                                                                        } else {

                                                                                            // --- 不在 ---
                                                                                            $bag->set([
                                                                                                'code' => trim($post['bagCode']),
                                                                                                'site_send' => $post['fromSiteId'],    // 为当前站点ID
                                                                                                'site_receive' => $post['siteId'],    //　从客户端获取的
                                                                                                'commission' => $post['commission'],
                                                                                                'state' => 0, //　0表待抢包
                                                                                                'count' => count($pieceArr),
                                                                                                'user_id' => '',    // 无
                                                                                                'time_add' => $_SERVER['REQUEST_TIME'],
                                                                                                'time_start' => strtotime($post['startTime']),
                                                                                                'time_end' => strtotime($post['endTime']),
                                                                                                'user_remove' => 0,
                                                                                                'worker_id' => $worker->id
                                                                                            ]);

                                                                                            $k = 0;
                                                                                            do {
                                                                                                if(++$k > 3) break;
                                                                                            } while(!$bag->create());
                                                                                        }

                                                                                        if($k <= 3) {

                                                                                            $preSack = Db::prepare('UPDATE ' . DB_PRE . 'sack SET worker_id=:worker_id ,count=0 , is_finish=1  WHERE id IN (' . $strSackIds . ') ');

                                                                                            if($preSack->execute([':worker_id' => $worker->id]) && $preSack->rowCount() > 0) {

                                                                                                if(count($pieceArr)) {

                                                                                                    $strPieceCodes = '"' . implode('","', $pieceArr) . '"';
                                                                                                    $stmt = Db::prepare('UPDATE ' . DB_PRE . 'order_piece SET  state_in=5 ,worker_id=:worker_id , bag_id=:bagId WHERE state_in<=4 AND state_in>=2 and state=1 and piece_code  IN (' . $strPieceCodes . ') ');

                                                                                                    if($stmt->execute([':worker_id' => $worker->id, ':bagId' => $bag->id]) && $stmt->rowCount() > 0) {
                                                                                                        \work\rallyPoint::addPieceStates($pieceArr, $worker->id, 5);
                                                                                                    } else
                                                                                                        $this->writeAesJson(0, '件信息已更新失败', 1); //
                                                                                                } else {
                                                                                                    Db::commit();
                                                                                                    $this->writeAesJson(0, '袋信息已更新但无件', 1); //　有已使用完的袋且无件提交
                                                                                                }
                                                                                            } else
                                                                                                $this->writeAesJson(0, '袋信息更新失败_，可能无此袋号或已是使用完的袋', 1);
                                                                                        } else
                                                                                            $this->writeJson(0, '包添加或修改失败');
                                                                                    } else
                                                                                        $this->writeJson(0, '提交袋所装件信息查询失败');
                                                                                } else
                                                                                    $this->writeAesJson(0, ['msg' => '提交的件状态有误', 'pieceCodes' => $result1], 1);
                                                                            } else
                                                                                $this->writeAesJson(0, '提交的件信息查询失败或件状态不符合要求', 1);
                                                                        } else
                                                                            $this->writeAesJson(0, ['msg' => '提交的袋状态有误', 'sackCodes' => $result], 1);
                                                                    } else
                                                                        $this->writeAesJson(0, '袋信息查询失败-，可能已装包或无此袋号', 1);
                                                                } else {
                                                                    // ---　无袋号时 ---

                                                                    if(count($pieces)) {
                                                                        $strPieceCodes = '"' . implode('","', $pieces) . '"';

                                                                        $stmt1 = Db::query('SELECT piece_code  FROM ' . DB_PRE . 'order_piece WHERE state_in<=4 AND state_in>=2 AND  piece_code IN (' . $strPieceCodes . ')'); //查询过滤符合要求的件

                                                                        if($stmt1 && $stmt1->rowCount() > 0) {
                                                                            while($data = $stmt1->fetch(\PDO::FETCH_ASSOC)) $pieceArr[] = strtoupper($data['piece_code']); // 合袋

                                                                            $result = array_diff($pieces, $pieceArr);

                                                                            if(!$result) {
                                                                                // --- 有件号 ---

                                                                                Db::beginTransaction();

                                                                                $bag = new \C\mod\bag();
                                                                                // --- 判断包码是否存在 ---
                                                                                if( $b=$bag->get( [['code','=',$post['bagCode']],['state' ,'in',[0,1]]])) {


                                                                                    // --- 已存在的包码，判断状态是否合法，添加 ---
                                                                                    $pre = Db::prepare('UPDATE cd_bag SET count=count+ :newCount WHERE id=:id');

                                                                                    if($pre->execute([':id'=> $b->id , ':newCount'=> count($pieceArr)] )) $k = 0;
                                                                                    else $k=4;
                                                                                    $bag = $b;
                                                                                } else {

                                                                                    $bag->set([
                                                                                        'code' => trim($post['bagCode']),
                                                                                        'site_send' => $post['fromSiteId'],    // 为当前站点ID
                                                                                        'site_receive' => $post['siteId'],    //　从客户端获取的
                                                                                        'commission' => $post['commission'],
                                                                                        'state' => 0, //　0表待抢包
                                                                                        'count' => count($pieceArr),
                                                                                        'user_id' => '',    // 无
                                                                                        'time_add' => $_SERVER['REQUEST_TIME'],
                                                                                        'time_start' => strtotime($post['startTime']),
                                                                                        'time_end' => strtotime($post['endTime']),
                                                                                        'user_remove' => 0,
                                                                                        'worker_id' => $worker->id
                                                                                    ]);
                                                                                    $k = 0;
                                                                                    do {
                                                                                        if(++$k > 3) break;
                                                                                    } while(!$bag->create());
                                                                                }

                                                                                if($k <= 3) {

                                                                                    $strPieceCodes = '"' . implode('","', $pieceArr) . '"';
                                                                                    $stmt = Db::prepare('UPDATE ' . DB_PRE . 'order_piece SET  state_in=5 ,worker_id=:worker_id , bag_id=:bagId  WHERE state_in<=4 AND state_in>=2 and state=1 and piece_code  IN (' . $strPieceCodes . ') ');
                                                                                    if($stmt->execute([':worker_id' => $worker->id, ':bagId' => $bag->id]) && $stmt->rowCount() > 0) {
                                                                                        \work\rallyPoint::addPieceStates($pieces, $worker->id, 5); // 添加 cd_piece_state 数据
                                                                                    } else
                                                                                        $this->writeAesJson(0, '件信息已更新失败', 1); //
                                                                                } else
                                                                                    $this->writeJson(0, '包添加或修改失败,可能该包码已添加过了');
                                                                            } else
                                                                                $this->writeAesJson(0, ['msg' => '提交的件状态有误', 'pieceCodes' => $result], 1);
                                                                        } else
                                                                            $this->writeAesJson(0, '件信息查询失败或件状态不符合要求', 1);
                                                                    } else
                                                                        // --- 无件号 ---
                                                                        $this->writeAesJson(0, '无袋号和件号提交', 1);
                                                                }
                                                            } else
                                                                $this->writeAesJson(0, '无佣金提交或提交格式不正确', 1);

                                                        } else
                                                            $this->writeAesJson(0, '终止时间不能小于等于始发时间', 1);
                                                    } else
                                                        $this->writeAesJson(0, '终止时间或始发时间应大于当前请求时间', 1);
                                                } else
                                                    $this->writeAesJson(0, '无终止时间提交', 1);
                                            } else
                                                $this->writeAesJson(0, '无始发时间提交', 1);
                                        } else
                                            $this->writeAesJson(0, '始发站点ID不能等于到达地站点ID', 1);
                                } else
                                    $this->writeAesJson(0, '无到达地站点ID提交', 1);
                            } else
                                $this->writeAesJson(0, '无始发站点ID提交', 1);
                        } else
                            $this->writeAesJson( 0 , '无包号提交' ,1 );
                    } else
                        $this->writeAesJson( 0 , '无件号或袋号提交' ,1 );
                }else
                    $this->writeAesJson( 0 , '非站点' ,1 );
            }

        }


        // --- 站点（派送）列表1 即将收录的包集---

        public function pullBagWillList() {

            if(($post = $this->getAesPost()) && ($worker = $this->getLogWorker())) {
                if( isset($post['siteId']) && ($post['siteId'] != '') ) {
                    if(isset($post['willLastId'])) {
                        $lastId1 = $post['willLastId'] + 0;
                        $lastId1 = $lastId1 <= 0 ? 0 : $lastId1;
                    } else {
                        $lastId1 = 0;
                    }
                    if($lastId1 == 0) $sqlLimit1 = ' cdb.id > 0 '; else  $sqlLimit1 = ' cdb.id < ' . $lastId1;

                    $stmt = Db::query('SELECT type FROM ' . DB_PRE . 'worker_type WHERE worker_id ="' . $worker->id . '" and type = 3');

                    if($stmt && $stmt->rowCount() > 0) {

                        $stmt2 = Db::query('SELECT cdb.count count , cdb.site_send siteSend , cdb.code code ,cdb.id id ,IF(isNull(cdu.phone),"0",cdu.phone) phone FROM cd_site_worker cdsw, cd_worker cdw , cd_site cds ,cd_bag cdb  LEFT JOIN  cd_user cdu ON cdu.id=cdb.user_id  WHERE cds.id=cdsw.site_id AND cdsw.site_id='.$post['siteId'].' AND cdw.id=cdsw.worker_id AND cdw.id=' . $worker->id . ' AND cdb.site_receive=cds.id AND cdb.state=2 AND ' . $sqlLimit1 . ' ORDER BY cdb.id DESC LIMIT  10 '); //  2 =>运送中,即将收录

                        $jbags = []; // 即将收录的包集
                        if($stmt2 && $stmt2->rowCount() > 0) {
                            while($data = $stmt2->fetch(\PDO::FETCH_ASSOC)) $jbags[] = $data;
                        }

                        $this->writeAesJson(1, ['jbags' => $jbags], 1);
                    } else
                        $this->writeAesJson(0, '非站点', 1);
                } else
                    $this->writeAesJson(0, '无当前所处站点ID提交', 1);
            }

        }

        // --- 站点（派送）列表1,已收录的件集 ---

        public function pullBagOkList() {

            if(($post = $this->getAesPost()) && ($worker = $this->getLogWorker())) {

                $stmt = Db::query('SELECT type FROM ' . DB_PRE . 'worker_type WHERE worker_id ="' . $worker->id . '" and type = 3');

                if($stmt && $stmt->rowCount() > 0) {

                    $sqlLimit2 = (!isset($post['okLastId'])) ?   ' cdo.area_id_recipient >= 0 ': ' cdo.area_id_recipient < '.$post['okLastId'];

                    $stmt2 = Db:: query('SELECT cdo.area_id_recipient  id , count(cdo.id) count , IF(isNull(cda.name),"未划分区",cda.name) name FROM cd_order_piece cdo LEFT JOIN cd_area cda ON cdo.area_id_recipient=cda.id   WHERE cdo.state_in=7 AND cdo.worker_id=' . $worker->id . '  AND  '. $sqlLimit2.'  GROUP BY cdo.area_id_recipient ORDER BY id DESC LIMIT  2 '); // state_in=5 表收录站点

                    $jPieces = [];
                    if($stmt2 && $stmt2->rowCount() > 0) {
                        while ($data = $stmt2->fetch(\PDO::FETCH_ASSOC))
                            $jPieces[] = $data; // 已收录的件集
                    }

                    $this->writeAesJson( 1 , [ 'jpieces' => $jPieces] , 1);
                } else
                    $this->writeAesJson( 0 , '非站点' ,1 );
            }

        }


        // --- 派送站点扫码接包 ---

        public function receiveBagAdd() {

            if(($post = $this->getAesPost()) && ($worker = $this->getLogWorker())) {

                $stmt = Db::query('SELECT TYPE FROM ' . DB_PRE . 'worker_type WHERE worker_id ="' . $worker->id . '" AND type=3');

                if($stmt && $stmt->rowCount() > 0) {

                    if( isset($post['bagCodes']) && count($post['bagCodes']) ) {
                        // --- 获取包ID ---
                        $strBagCodes = '"' . implode('","', $post['bagCodes']) . '"';

                        $stmt1 = Db::query('SELECT id , user_id ,commission ,code FROM ' . DB_PRE . 'bag WHERE  state=2  and code IN (' . $strBagCodes . ') ');

                        if($stmt1 && $stmt1->rowCount() > 0) {

                            $bagIds = [] ; $bagCodes = [];
                            while($data = $stmt1->fetch(\PDO::FETCH_ASSOC)) {
                                $bagIds[] = $data['id']; // 取过滤后的包ID集
                                $bagCodes[] = $data['code'];
                            }

                            $result = array_diff($post['bagCodes'], $bagCodes);
                            if(!$result) {

                                // --- 获取包费用
                                $stmt1 = Db::query('SELECT cdb.id id, user_id ,sum(commission) commission ,cdb.code code, cds.name site_send ,cdc.name city_send ,cds2.name site_receive , cdc2.name city_receive FROM cd_bag cdb ,cd_site cds , cd_city cdc ,cd_site cds2 ,cd_city cdc2 WHERE  cdb.site_send=cds.id AND cds.city_id=cdc.id AND cdb.site_receive=cds2.id AND cds2.city_id=cdc2.id AND cdb.state=2  AND cdb.code IN (' . $strBagCodes . ')  GROUP BY user_id');

                                if($stmt1 && $stmt1->rowCount() > 0) {

                                    $bagUserIds = [];
                                    $bagCommis = [];
                                    while($data = $stmt1->fetch(\PDO::FETCH_ASSOC)) {
                                        $bagUserIds[] = $data['user_id'];
                                        $bagCommis[] = $data['commission'] >= 0 ? $data['commission'] : 0 ; // 包费用
                                        $bagSend[] = $data['city_send'].$data['site_send'];
                                        $bagRece[] = $data['city_receive'].$data['site_receive'];
                                    }

                                    $strBagIds = '"' . implode('","', $bagIds) . '"';

                                    $stmt1 = Db::query('SELECT id FROM ' . DB_PRE . 'order_piece WHERE  state_in=6  and bag_id IN (' . $strBagIds . ') ');
                                    $pieceIds = [];
                                    while($data = $stmt1->fetch(\PDO::FETCH_ASSOC)) $pieceIds[] = $data['id']; // 取过滤后的包ID集

                                    Db::beginTransaction();

                                    // --- 更新包信息 ---

                                    $prebag = Db::prepare('UPDATE ' . DB_PRE . 'bag SET worker_id=:worker_id ,state=3 WHERE state=2 AND id IN (' . $strBagIds . ') ');

                                    if($prebag->execute([':worker_id' => $worker->id]) && $prebag->rowCount() > 0) {

                                        // --- 更新件信息 ---
                                        $stmt = Db::prepare('UPDATE cd_order_piece SET state_in=7  ,worker_id=:worker_id WHERE state_in =6  AND bag_id IN (' . $strBagIds . ') ');
                                        if($stmt->execute([':worker_id' => $worker->id]) && ($stmt->rowCount() >= 0)) {

                                            // --- 更新用户余额 ---

                                            $ids = implode(',', $bagUserIds);
                                            $datesArr = [];
                                            $sql = "UPDATE cd_user SET price =  price + CASE id ";
                                            foreach($bagUserIds as $k => $v) {
                                                $sql .= ' WHEN :id' . $k . ' THEN :commis' . $k;
                                                $datesArr = array_merge($datesArr, [':id' . $k => $v, ':commis' . $k => $bagCommis[$k]]);
                                            }
                                            $sql .= " END WHERE id IN ($ids)";

                                            $preUser = Db::prepare($sql);

                                            if($preUser->execute($datesArr) && $preUser->rowCount() > 0) {

                                                // --- 添加用户获取金额信息到帐单中
                                                $bdates = [];
                                                foreach($bagUserIds as $k=>$v)
                                                    $bdates[] = [
                                                    'user_ids'=>$bagUserIds[$k] ,
                                                    'money' =>$bagCommis[$k],
                                                    'title'=> '捎带赚钱佣金',
                                                    'content'=> $bagSend[$k].'->'.$bagRece[$k].' 捎带行李',
                                                    'time_add'=> $_SERVER['REQUEST_TIME'],
                                                    //'pay_type=> ,// -----暂定
                                                    'pm_type'=> 0 , // ０表负
                                                    'type' => 0 //2表提现到银行卡的钱
                                                ];
                                                $bkeyArr = array_keys($bdates[0]);
                                                $bills = new bill();
                                                $k = 0;
                                                do{
                                                    if( $k++ >= 3 ){ break;}   // 3次失败机会
                                                    $result = $bills->insert($bkeyArr , $bdates);
                                                }while(!$result);

                                                if($result){
                                                    // --- 添加　件状态数据
                                                    rallyPoint::addPieceStates($pieceIds, $worker->id, 7, true);  // 7表已下车
                                                } else {
                                                    Db::rollBack();
                                                    $this->writeAesJson(0, '帐单信息保存失败');
                                                }
                                            } else {
                                                Db::rollBack();
                                                $this->writeAesJson(0, '用户余额修改失败', 1);
                                            }
                                        } else
                                            $this->writeAesJson(0, '件状态更新失败', 1);
                                    } else
                                        $this->writeAesJson(0, '包状态更新失败', 1);
                                } else
                                    $this->writeAesJson(0, '获取包费用失败', 1);
                            } else
                                $this->writeAesJson(0 ,['msg' => '提交的包状态有误', 'bagCodes' => $result], 1);
                        } else
                            $this->writeAesJson(0, '查无包码或已接此包', 1);
                    } else
                        $this->writeAesJson(0, '无包码上传', 1);
                } else
                    $this->writeAesJson(0, '非站点', 1);
            }

        }


        // --- 返回工作人员负责的站点名和ＩＤ ---
        public function workerSites() {

            if(($post = $this->getAesPost()) && ($worker = $this->getLogWorker())) {

                $stmt = Db::query('SELECT TYPE FROM ' . DB_PRE . 'worker_type WHERE worker_id ="' . $worker->id . '" AND type=3');

                if($stmt && $stmt->rowCount() > 0) {

                    $stmt = Db::query('SELECT site_id siteId , name FROM ' . DB_PRE . 'site_worker sw ,'. DB_PRE . 'site s WHERE sw.site_id=s.id AND worker_id ="' . $worker->id . '"');
                    $workerSites = [];
                    while($data = $stmt->fetch(\PDO::FETCH_ASSOC)) $workerSites[] = $data;

                    $this->writeAesJson( 1 , ['jworkerSites' => $workerSites] , 1);
                } else
                    $this->writeAesJson(0, '非站点', 1);
            }
        }

    }
    
}

